import Vue from 'vue'
import VueRouter from 'vue-router'
// import HomeView from '../views/HomeView.vue'
import Home from '../views/Home/home.vue'

Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  // {
  //   path: '/',
  //   name: 'home',
  //   component: HomeView
  // },
  {
    path: '/',
    redirect:'home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import('@/views/Goods/Goods.vue')
  },
  {
    path: '/goodsinfo',
    name: 'goodsinfo',
    component: () => import('@/views/GoodsInfo/GoodsInfo.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login.vue')
  },
  {
    path: '/merchants',
    name: 'merchants',
    
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../views/merchants/merchants.vue'),
    children: [
      {
        path: '/',
        redirect:'merInfor'
        
      },
      {
        name:'merInfor',
        path: 'merInfor',
        component: () => import('../views/merchants/merInfo/merInfo.vue'),
        meta: { title: '商家信息' }
      },
      {
        name:'mergoods',
        path: 'mergoods',
        component: () => import('../views/merchants/mergoods/mergoods.vue'),
        meta: { title: '商品列表' }
      },
      {
        name:'merorder',
        path: 'merorder',
        component: () => import('../views/merchants/merorder/merorder.vue'),
        meta: { title: '订单列表' }
      },
      {
        name:'goodstype',
        path: 'goodstype',
        component: () => import('../views/merchants/goodstype/goodstype.vue'),
        meta: { title: '商品类型' }
      }
      

    ]
  },
  {
    path: '/usercenter',
    name: 'usercenter',
    component: () => import('../views/userCenter/index.vue'),
    children: [
      {
        path: '/',
        redirect:'userInfor'
        
      },
      {
        name:'userInfor',
        path: 'userInfor',
        component: () => import('../views/userCenter/userinfo.vue'),
        meta: { title: '个人信息' }
      },
      
      {
        name:'userorder',
        path: 'userorder',
        component: () => import('../views/userCenter/userorder.vue'),
        meta: { title: '订单列表' }
      },

    ]
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
]

// const router = new VueRouter({
//   // mode: 'history',
//   // base: process.env.BASE_URL,
//   routes
// })
const createRouter = () => new VueRouter({
  // mode: 'history',
  // base: process.env.BASE_URL,
  routes: routes
})
const router = createRouter()
//写一个重置路由的方法，切换用户后，或者退出时清除动态加载的路由
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 新路由实例matcer，赋值给旧路由实例的matcher，（相当于replaceRouter）
}
const RouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return RouterPush.call(this, to).catch(err => err)
}
const RouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace (to) {
  return RouterReplace.call(this, to).catch(err => err)
}

export default router
